import router from 'umi/router';
import { message } from 'antd';
import {
  fakeUserInfo,
  fakeModifyPsw,
  queryUserInfo,
  queryAllUsers,
  saveUsersDel,
  saveUsersAdd,
  saveUsersEdit,
  setUserCooike,
  updateUserCooike,
} from '../services/user';
import { queryAllRole } from '../services/role';

export default {
  namespace: 'user',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    roleListSelect:[],
    loading: {
      info:true,
      reslist: true,
      model_submit:false,
      add:false,
    },
  },

  effects: {

    *submitUserInfoForm({ payload }, { call, put }) {
      // put触发action，call调用异步支持逻辑，
      // select从state中获取数据 const todos= yield select(state => state.todos)
      // 路由跳转部分可以这样实现 yield put(router.push('/form/step-form/result'))
      yield put({
        type: 'changeLoading',
        payload: {model_submit: true},
      });
      const response = yield call(fakeUserInfo, payload);
      response &&  (
        yield call(updateUserCooike,{email:payload.email,mobile:payload.mobile})
      )
      yield put({
        type: 'changeLoading',
        payload: {model_submit: false},
      });
      response &&  (
        message.success('用户信息修改成功！')
      )
    },

    *submitModifyPswForm({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: {model_submit: true},
      });
      const response = yield call(fakeModifyPsw, payload);
      yield put({
        type: 'changeLoading',
        payload: {model_submit: false},
      });
      response &&  (
        message.success('用户密码修改成功！')
      )
    },

    // 管理员获取用户列表
    *fetch_user_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{reslist:true}
      })
      const response =yield call(queryAllUsers, payload);
      response &&  (
        yield put({
          type: 'appendData',
          payload:response.data,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{reslist:false}
      })
    },

    // 管理员删除用户
    *action_user_del({ payload }, { call, put }) {
      const response =yield call(saveUsersDel, payload);
    },


    // 添加 用户信息
    *add_user({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{add:true}
      })
      const response =yield call(saveUsersAdd, payload);
      yield put({
        type: 'changeLoading',
        payload:{add:false}
      })
      response &&  router.push(`/system/users/list`)
    },

    // 修改 用户信息
    *edit_user({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{add:true}
      })
      const response =yield call(saveUsersEdit, payload);
      yield put({
        type: 'changeLoading',
        payload:{add:false}
      })
      response &&  router.push(`/system/users/list`)
    },



    // 获取 用户详情
    *fetch_user_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{info:true}
      })
      const response =yield call(queryUserInfo, payload);
      response &&  (
        yield put({
          type: 'appendDataDetail',
          payload:response.data
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{info:false}
      })
    },




    *fetch_role_nopage({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{reslist:true}
      })
      const response =yield call(queryAllRole, payload);
      response &&  (
        yield put({
          type: 'appendRoleListData',
          payload:response.data.list,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{reslist:false}
      })
    },

  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendRoleListData(state, action) {
      return {
        ...state,
        roleListSelect: action.payload,
      };
    },

    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendCurrentData(state, action) {
      return {
        ...state,
        currentUser: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        roleListSelect:[],
        detaildata:{},
        loading:{}
      };
    },
  },
};
